﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WzYtGl : PageBase
    {
        /// <summary>
        /// 加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (Page.IsPostBack) { return; }
            BtnAdd.Visible = IsAccess("编辑物资用途");
            if (Request.QueryString["sWhere"] != null)
            {
                BindData(Request.QueryString["sWhere"], Request.QueryString["nPage"]);
            }
            TextWhere.Focus();
        }

        /// <summary>
        /// 绑定刷新数据
        /// </summary>
        private void BindData(string sWhere, string nPage)
        {
            sWhere = (sWhere == null ? string.Empty : sWhere.Trim());
            // 设置页数
            DataSet dsCount = BJcSj.GetWzYtByCxCount(sWhere);
            Pager1.RecordCount = (dsCount == null ? 0 : (int)dsCount.Tables[0].Rows[0][0]);
            Pager1.CurrentPageIndex = (nPage == null ? 1 : int.Parse(nPage));
            // 查询数据
            DataSet ds = BJcSj.GetWzYtByCx(sWhere, Pager1.PageSize, Pager1.CurrentPageIndex);
            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                PanelList1.Visible = true;
                PanelList2.Visible = false;
                return;
            }
            PanelList1.Visible = false;
            PanelList2.Visible = true;
            // 绑定表格
            GridData.DataSource = ds;
            GridData.DataBind();
            GridData.SelectedIndex = -1;
            // 设置操作权限
            if (!IsAccess("编辑物资用途"))
            {
                for (int i = 0; i < GridData.Rows.Count; i++)
                {
                    GridData.Rows[i].Cells[4].Controls[3].Visible = false;
                    GridData.Rows[i].Cells[4].Controls[5].Visible = false;
                    GridData.Rows[i].Cells[4].Controls[7].Visible = false;
                }
            }
        }

        /// <summary>
        /// 操作[编辑/删除]
        /// </summary>
        protected void GridData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "bj")
            {
                int iSelectId = Convert.ToInt32(GridData.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                BindData(Request.QueryString["sWhere"], Request.QueryString["nPage"]);
                for (int idx = 0; idx < GridData.DataKeys.Count; idx++)
                {
                    if ((int)GridData.DataKeys[idx].Value == iSelectId)
                    {
                        GridData.SelectedIndex = idx;
                        break;
                    }
                }
            }
            else if (e.CommandName == "sc" && IsAccess("编辑物资用途"))
            {
                if (ShowErr(BJcSj.DelWzYt((int)GridData.DataKeys[Convert.ToInt32(e.CommandArgument)].Value)))
                {
                    return;
                }
                BindData(Request.QueryString["sWhere"], Request.QueryString["nPage"]);
                GridData.SelectedIndex = (Convert.ToInt32(e.CommandArgument) < GridData.Rows.Count ? Convert.ToInt32(e.CommandArgument) : GridData.Rows.Count - 1);
            }
        }
    }
}